import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { VenueBookingApi } from '#/api/cmt/venue/booking';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getVenueInfoOptions } from '#/api/cmt/venue/info';
import { getRangePickerDefaultProps } from '#/utils';

export const AUDIT_STATUS_OPTIONS = [{ label: '待审核', value: 0 }, { label: '通过', value: 1 }, { label: '拒绝', value: 2 }];
export const USE_STATUS_OPTIONS = [{ label: '未开始', value: 0 }, { label: '使用中', value: 1 }, { label: '已结束', value: 2 }, { label: '已取消', value: 3 }];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'bookingNo',
      label: '预约编号',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入预约编号',
      },
    },
    {
      fieldName: 'venueId',
      label: '阵地',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getVenueInfoOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择阵地',
      },
    },
    {
      fieldName: 'userId',
      label: '预约会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full',
      },
    },
    {
      fieldName: 'bookingDate',
      label: '预约日期',
      rules: 'required',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择预约日期',
      },
    },
    {
      fieldName: 'startTime',
      label: '开始时间',
      rules: 'required',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        showTime: true,
        placeholder: '请选择开始时间',
      },
    },
    {
      fieldName: 'endTime',
      label: '结束时间',
      rules: 'required',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        showTime: true,
        placeholder: '请选择结束时间',
      },
    },
    {
      fieldName: 'purpose',
      label: '使用用途',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入使用用途',
      },
    },
    {
      fieldName: 'participants',
      label: '参与人数',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full', min: 1,
      },
    },
    {
      fieldName: 'contactName',
      label: '联系人',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'useStatus',
      label: '使用状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: USE_STATUS_OPTIONS,
        placeholder: '请选择使用状态',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<VenueBookingApi.VenueBooking>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'bookingNo',
      title: '预约编号',
      minWidth: 120,
    },
    {
      field: 'venueId',
      title: '阵地ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '会员ID',
      minWidth: 120,
    },
    {
      field: 'bookingDate',
      title: '预约日期',
      minWidth: 120,
    },
    {
      field: 'startTime',
      title: '开始时间',
      minWidth: 120,
    },
    {
      field: 'endTime',
      title: '结束时间',
      minWidth: 120,
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 120,
    },
    {
      field: 'useStatus',
      title: '使用状态',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
